var router = require("express").Router();

var User = require("../modules/db/user");
var md5 = require("md5");

router.post("/login", (req, res) => {
  User.findOne({ username: req.body.username }, (err, user) => {
    if (user) {
      if (md5(req.body.password) == user.password) {
        req.session.user = JSON.parse(JSON.stringify(user));
        res.json({
          error: 0,
          txt: "登录成功",
        });
      } else {
        res.json({
          error: 1,
          txt: "密码错误",
        });
      }
    } else {
      res.json({
        error: 1,
        txt: "账号不存在",
      });
    }
  });
});

module.exports = router;
